package Aaaaaa;

class ListNode{
  int val;
  ListNode next = null;
  public ListNode(int val) {
    this.val = val;
  }
}

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param a ListNode类一维数组 指向这些数链的开头
     * @return ListNode类
     */
    public ListNode solve (ListNode[] a) {
        // write code here
        ListNode res=new ListNode(0);
        ListNode cur=res;
        int length=a.length;
        int max=Integer.MIN_VALUE;
        for(int i=0;i<length;i++){
            max=Math.max(max,getSize(a[i]));
        }
        for(int x=0;x<max;x++) {
            for (int i = 0; i < length; i++) {
                if (a[i] != null) {
                    cur.next = new ListNode(a[i].val);
                    a[i] = a[i].next;
                    cur = cur.next;
                }
            }
        }
        return res.next;
    }

    private int getSize(ListNode listNode) {
        int count=0;
        while(listNode!=null){
            count++;
            listNode=listNode.next;
        }
        return count;
    }
}

